package com.fhsk.file.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.Properties;

/**
 * @author NealZhi
 * @version 1.0.0
 * @Description: 配置读取工具类(适用于工具类读取配置文件, 如果已经spring所管理, 请使用 @ Value注解)
 * @date 2020-07-05
 */
@Slf4j
public class ReadYmlUtil {
    private static Properties ymlProperties = new Properties();

    private static Properties activeYmlProperties = new Properties();

    public ReadYmlUtil(Properties properties, Properties activeProperties) {
        ymlProperties = properties;
        activeYmlProperties = activeProperties;
    }


    public static String getValue(String key) {
        if (ymlProperties.containsKey(key)) {
            return ymlProperties.getProperty(key);
        }

        if (activeYmlProperties.containsKey(key)) {
            return activeYmlProperties.getProperty(key);
        }
        return "";
    }

    public static Long getLong(String key) {
        try {
            if (ymlProperties.containsKey(key)) {
                String property = ymlProperties.getProperty(key);
                property = property.replace("\"", "");
                return Long.parseLong(property);
            }

            if (activeYmlProperties.containsKey(key)) {
                String property = activeYmlProperties.getProperty(key);
                property = property.replace("\"", "");
                return Long.parseLong(property);
            }
        } catch (Exception e) {
            //do nothing
        }
        return null;
    }
}
